﻿using System.Collections.Generic;

namespace BorgExt.Behaviors
{
    public class ReplaceRule<T> : Rule<T>
    {
        private readonly int _capacity;
        private readonly LinkedList<T> _queue;

        public ReplaceRule()
            : this(1)
        { }

        public ReplaceRule(int capacity)
        {
            _queue = new LinkedList<T>();
            _capacity = capacity;
        }

        public override void Composed(T item)
        {
            _queue.AddFirst(item);
        }

        public override void Composing(T item)
        {
            if (_queue.Count == _capacity)
                OnDecompose(_queue.Last.Value);
        }

        public override void Decomposed(T item)
        {
            _queue.Remove(item);
        }
    }
}

